package com.gyf.usercenter.ui.activity

import android.os.Bundle
import android.view.View
import com.gyf.base.common.AppManager
import com.gyf.base.ext.enable
import com.gyf.base.ext.onClick
import com.gyf.base.ui.activity.BaseMvpActivity
import com.gyf.usercenter.R
import com.gyf.usercenter.injection.component.DaggerUserComponent
import com.gyf.usercenter.injection.module.UserModule
import com.gyf.usercenter.ui.presenter.ResetPwdPresenter
import com.gyf.usercenter.ui.presenter.view.ResetPwdView
import kotlinx.android.synthetic.main.activity_reset_pwd.*
import org.jetbrains.anko.toast

class ResetPwdActivity : BaseMvpActivity<ResetPwdPresenter>(), ResetPwdView {
    override fun onResetPwdResult(result: String?) {
        toast("修改成功")
        AppManager.instance.finishActivityByName(ForgetPwdActivity::class.java);
        finish();
    }


    override fun injectComponent() {
        DaggerUserComponent.builder().activityComponent(activityComponent).userModule(UserModule()).build().inject(this);
        mPresenter.mView = this;
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_reset_pwd)
        initView();
    }

    private fun initView() {
        mConfirmBtn.enable(mPwdEt) { isBtnEnable() };
        mConfirmBtn.enable(mPwdConfirmEt) { isBtnEnable() };
        mConfirmBtn.onClick {
            mPresenter.resetPwd(intent.getStringExtra("mobile"), mPwdConfirmEt.text.toString());
        };
    }

    private fun isBtnEnable(): Boolean {
        return mPwdEt.text.isNullOrEmpty().not() &&
                mPwdConfirmEt.text.isNullOrEmpty().not();
    }
}
